package org.example.forum.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.example.forum.common.AppResult;
import org.example.forum.common.ResultCode;
import org.example.forum.config.AppConfig;
import org.example.forum.model.Article;
import org.example.forum.model.ArticleReply;
import org.example.forum.model.User;
import org.example.forum.services.IArticleReplyService;
import org.example.forum.services.IArticleService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@Tag(name = ("回复接口"))
@RequestMapping("/reply")
public class ArticleReplyController {
    @Resource
    private IArticleReplyService articleReplyService;

    @Resource
    private IArticleService articleService;


    /**
     *
     * @return
     */
    @Operation(summary = "回复帖子")
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @Parameter(ref = "帖子id")@RequestParam("articleId")@NonNull Long articleId,
                            @Parameter(ref = "帖子正文")@RequestParam("content")@NonNull String content) {
        //1、获取用户校验用户信息
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        //判断用户状态是否正常
        if (user.getState() == 1) {
            //表示禁言
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //获取要回复的帖子信息
        Article article = articleService.selectById(articleId);
        if (article == null || article.getDeleteState() == 1) {
            //帖子已删除或不存在
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        //校验帖子状态(是否封贴
        if (article.getState() == 1) {
            return AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED);
        }
        //构建回复对象
        ArticleReply articleReply = new ArticleReply();
        articleReply.setArticleId(articleId);       //要回复的帖子id
        articleReply.setContent(content);           //回复的内容
        articleReply.setPostUserId(user.getId());   //回复的发送者
        //调用service，写入回复信息
        articleReplyService.create(articleReply);
        return AppResult.success();
    }

    @Operation(summary = "获取帖子列表")
    @GetMapping("/getReplies")
    public AppResult<List<ArticleReply>> getRepliesByArticleId (@Parameter(ref = "帖子id")@RequestParam("articleId")@NonNull Long articleId) {
        //校验帖子是否存在
        Article article = articleService.selectById(articleId);
        if (article == null || article.getDeleteState() == 1) {
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        //调用service获取结果
        List<ArticleReply> result = articleReplyService.selectByArticleId(articleId);
        //返回结果
        return AppResult.success(result);
    }

}
